/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gruposp2p.aularest.model.controller;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import org.gruposp2p.aularest.model.Course;
import org.gruposp2p.aularest.model.Coursegroup;
import org.gruposp2p.aularest.model.Subject;
import java.util.ArrayList;
import java.util.Collection;
import org.gruposp2p.aularest.model.Itemcalificable;
import org.gruposp2p.aularest.model.Student;
import org.gruposp2p.aularest.model.controller.exceptions.IllegalOrphanException;
import org.gruposp2p.aularest.model.controller.exceptions.NonexistentEntityException;
import org.gruposp2p.aularest.model.controller.exceptions.PreexistingEntityException;

/**
 *
 * @author jj
 */
public class CoursegroupJpaController {

    public CoursegroupJpaController() {
        emf = Persistence.createEntityManagerFactory("AulaRest_PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Coursegroup coursegroup) throws PreexistingEntityException, Exception {
        if (coursegroup.getSubjectCollection() == null) {
            coursegroup.setSubjectCollection(new ArrayList<Subject>());
        }
        if (coursegroup.getItemcalificableCollection() == null) {
            coursegroup.setItemcalificableCollection(new ArrayList<Itemcalificable>());
        }
        if (coursegroup.getStudentCollection() == null) {
            coursegroup.setStudentCollection(new ArrayList<Student>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Course courseId = coursegroup.getCourseId();
            if (courseId != null) {
                courseId = em.getReference(courseId.getClass(), courseId.getId());
                coursegroup.setCourseId(courseId);
            }
            Collection<Subject> attachedSubjectCollection = new ArrayList<Subject>();
            for (Subject subjectCollectionSubjectToAttach : coursegroup.getSubjectCollection()) {
                subjectCollectionSubjectToAttach = em.getReference(subjectCollectionSubjectToAttach.getClass(), subjectCollectionSubjectToAttach.getId());
                attachedSubjectCollection.add(subjectCollectionSubjectToAttach);
            }
            coursegroup.setSubjectCollection(attachedSubjectCollection);
            Collection<Itemcalificable> attachedItemcalificableCollection = new ArrayList<Itemcalificable>();
            for (Itemcalificable itemcalificableCollectionItemcalificableToAttach : coursegroup.getItemcalificableCollection()) {
                itemcalificableCollectionItemcalificableToAttach = em.getReference(itemcalificableCollectionItemcalificableToAttach.getClass(), itemcalificableCollectionItemcalificableToAttach.getId());
                attachedItemcalificableCollection.add(itemcalificableCollectionItemcalificableToAttach);
            }
            coursegroup.setItemcalificableCollection(attachedItemcalificableCollection);
            Collection<Student> attachedStudentCollection = new ArrayList<Student>();
            for (Student studentCollectionStudentToAttach : coursegroup.getStudentCollection()) {
                studentCollectionStudentToAttach = em.getReference(studentCollectionStudentToAttach.getClass(), studentCollectionStudentToAttach.getId());
                attachedStudentCollection.add(studentCollectionStudentToAttach);
            }
            coursegroup.setStudentCollection(attachedStudentCollection);
            em.persist(coursegroup);
            if (courseId != null) {
                courseId.getCoursegroupCollection().add(coursegroup);
                courseId = em.merge(courseId);
            }
            for (Subject subjectCollectionSubject : coursegroup.getSubjectCollection()) {
                subjectCollectionSubject.getCoursegroupCollection().add(coursegroup);
                subjectCollectionSubject = em.merge(subjectCollectionSubject);
            }
            for (Itemcalificable itemcalificableCollectionItemcalificable : coursegroup.getItemcalificableCollection()) {
                Coursegroup oldCoursegroupIdOfItemcalificableCollectionItemcalificable = itemcalificableCollectionItemcalificable.getCoursegroupId();
                itemcalificableCollectionItemcalificable.setCoursegroupId(coursegroup);
                itemcalificableCollectionItemcalificable = em.merge(itemcalificableCollectionItemcalificable);
                if (oldCoursegroupIdOfItemcalificableCollectionItemcalificable != null) {
                    oldCoursegroupIdOfItemcalificableCollectionItemcalificable.getItemcalificableCollection().remove(itemcalificableCollectionItemcalificable);
                    oldCoursegroupIdOfItemcalificableCollectionItemcalificable = em.merge(oldCoursegroupIdOfItemcalificableCollectionItemcalificable);
                }
            }
            for (Student studentCollectionStudent : coursegroup.getStudentCollection()) {
                Coursegroup oldCoursegroupIdOfStudentCollectionStudent = studentCollectionStudent.getCoursegroupId();
                studentCollectionStudent.setCoursegroupId(coursegroup);
                studentCollectionStudent = em.merge(studentCollectionStudent);
                if (oldCoursegroupIdOfStudentCollectionStudent != null) {
                    oldCoursegroupIdOfStudentCollectionStudent.getStudentCollection().remove(studentCollectionStudent);
                    oldCoursegroupIdOfStudentCollectionStudent = em.merge(oldCoursegroupIdOfStudentCollectionStudent);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCoursegroup(coursegroup.getId()) != null) {
                throw new PreexistingEntityException("Coursegroup " + coursegroup + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Coursegroup coursegroup) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Coursegroup persistentCoursegroup = em.find(Coursegroup.class, coursegroup.getId());
            Course courseIdOld = persistentCoursegroup.getCourseId();
            Course courseIdNew = coursegroup.getCourseId();
            Collection<Subject> subjectCollectionOld = persistentCoursegroup.getSubjectCollection();
            Collection<Subject> subjectCollectionNew = coursegroup.getSubjectCollection();
            Collection<Itemcalificable> itemcalificableCollectionOld = persistentCoursegroup.getItemcalificableCollection();
            Collection<Itemcalificable> itemcalificableCollectionNew = coursegroup.getItemcalificableCollection();
            Collection<Student> studentCollectionOld = persistentCoursegroup.getStudentCollection();
            Collection<Student> studentCollectionNew = coursegroup.getStudentCollection();
            List<String> illegalOrphanMessages = null;
            for (Itemcalificable itemcalificableCollectionOldItemcalificable : itemcalificableCollectionOld) {
                if (!itemcalificableCollectionNew.contains(itemcalificableCollectionOldItemcalificable)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Itemcalificable " + itemcalificableCollectionOldItemcalificable + " since its coursegroupId field is not nullable.");
                }
            }
            for (Student studentCollectionOldStudent : studentCollectionOld) {
                if (!studentCollectionNew.contains(studentCollectionOldStudent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Student " + studentCollectionOldStudent + " since its coursegroupId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (courseIdNew != null) {
                courseIdNew = em.getReference(courseIdNew.getClass(), courseIdNew.getId());
                coursegroup.setCourseId(courseIdNew);
            }
            Collection<Subject> attachedSubjectCollectionNew = new ArrayList<Subject>();
            for (Subject subjectCollectionNewSubjectToAttach : subjectCollectionNew) {
                subjectCollectionNewSubjectToAttach = em.getReference(subjectCollectionNewSubjectToAttach.getClass(), subjectCollectionNewSubjectToAttach.getId());
                attachedSubjectCollectionNew.add(subjectCollectionNewSubjectToAttach);
            }
            subjectCollectionNew = attachedSubjectCollectionNew;
            coursegroup.setSubjectCollection(subjectCollectionNew);
            Collection<Itemcalificable> attachedItemcalificableCollectionNew = new ArrayList<Itemcalificable>();
            for (Itemcalificable itemcalificableCollectionNewItemcalificableToAttach : itemcalificableCollectionNew) {
                itemcalificableCollectionNewItemcalificableToAttach = em.getReference(itemcalificableCollectionNewItemcalificableToAttach.getClass(), itemcalificableCollectionNewItemcalificableToAttach.getId());
                attachedItemcalificableCollectionNew.add(itemcalificableCollectionNewItemcalificableToAttach);
            }
            itemcalificableCollectionNew = attachedItemcalificableCollectionNew;
            coursegroup.setItemcalificableCollection(itemcalificableCollectionNew);
            Collection<Student> attachedStudentCollectionNew = new ArrayList<Student>();
            for (Student studentCollectionNewStudentToAttach : studentCollectionNew) {
                studentCollectionNewStudentToAttach = em.getReference(studentCollectionNewStudentToAttach.getClass(), studentCollectionNewStudentToAttach.getId());
                attachedStudentCollectionNew.add(studentCollectionNewStudentToAttach);
            }
            studentCollectionNew = attachedStudentCollectionNew;
            coursegroup.setStudentCollection(studentCollectionNew);
            coursegroup = em.merge(coursegroup);
            if (courseIdOld != null && !courseIdOld.equals(courseIdNew)) {
                courseIdOld.getCoursegroupCollection().remove(coursegroup);
                courseIdOld = em.merge(courseIdOld);
            }
            if (courseIdNew != null && !courseIdNew.equals(courseIdOld)) {
                courseIdNew.getCoursegroupCollection().add(coursegroup);
                courseIdNew = em.merge(courseIdNew);
            }
            for (Subject subjectCollectionOldSubject : subjectCollectionOld) {
                if (!subjectCollectionNew.contains(subjectCollectionOldSubject)) {
                    subjectCollectionOldSubject.getCoursegroupCollection().remove(coursegroup);
                    subjectCollectionOldSubject = em.merge(subjectCollectionOldSubject);
                }
            }
            for (Subject subjectCollectionNewSubject : subjectCollectionNew) {
                if (!subjectCollectionOld.contains(subjectCollectionNewSubject)) {
                    subjectCollectionNewSubject.getCoursegroupCollection().add(coursegroup);
                    subjectCollectionNewSubject = em.merge(subjectCollectionNewSubject);
                }
            }
            for (Itemcalificable itemcalificableCollectionNewItemcalificable : itemcalificableCollectionNew) {
                if (!itemcalificableCollectionOld.contains(itemcalificableCollectionNewItemcalificable)) {
                    Coursegroup oldCoursegroupIdOfItemcalificableCollectionNewItemcalificable = itemcalificableCollectionNewItemcalificable.getCoursegroupId();
                    itemcalificableCollectionNewItemcalificable.setCoursegroupId(coursegroup);
                    itemcalificableCollectionNewItemcalificable = em.merge(itemcalificableCollectionNewItemcalificable);
                    if (oldCoursegroupIdOfItemcalificableCollectionNewItemcalificable != null && !oldCoursegroupIdOfItemcalificableCollectionNewItemcalificable.equals(coursegroup)) {
                        oldCoursegroupIdOfItemcalificableCollectionNewItemcalificable.getItemcalificableCollection().remove(itemcalificableCollectionNewItemcalificable);
                        oldCoursegroupIdOfItemcalificableCollectionNewItemcalificable = em.merge(oldCoursegroupIdOfItemcalificableCollectionNewItemcalificable);
                    }
                }
            }
            for (Student studentCollectionNewStudent : studentCollectionNew) {
                if (!studentCollectionOld.contains(studentCollectionNewStudent)) {
                    Coursegroup oldCoursegroupIdOfStudentCollectionNewStudent = studentCollectionNewStudent.getCoursegroupId();
                    studentCollectionNewStudent.setCoursegroupId(coursegroup);
                    studentCollectionNewStudent = em.merge(studentCollectionNewStudent);
                    if (oldCoursegroupIdOfStudentCollectionNewStudent != null && !oldCoursegroupIdOfStudentCollectionNewStudent.equals(coursegroup)) {
                        oldCoursegroupIdOfStudentCollectionNewStudent.getStudentCollection().remove(studentCollectionNewStudent);
                        oldCoursegroupIdOfStudentCollectionNewStudent = em.merge(oldCoursegroupIdOfStudentCollectionNewStudent);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = coursegroup.getId();
                if (findCoursegroup(id) == null) {
                    throw new NonexistentEntityException("The coursegroup with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Coursegroup coursegroup;
            try {
                coursegroup = em.getReference(Coursegroup.class, id);
                coursegroup.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The coursegroup with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Itemcalificable> itemcalificableCollectionOrphanCheck = coursegroup.getItemcalificableCollection();
            for (Itemcalificable itemcalificableCollectionOrphanCheckItemcalificable : itemcalificableCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Coursegroup (" + coursegroup + ") cannot be destroyed since the Itemcalificable " + itemcalificableCollectionOrphanCheckItemcalificable + " in its itemcalificableCollection field has a non-nullable coursegroupId field.");
            }
            Collection<Student> studentCollectionOrphanCheck = coursegroup.getStudentCollection();
            for (Student studentCollectionOrphanCheckStudent : studentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Coursegroup (" + coursegroup + ") cannot be destroyed since the Student " + studentCollectionOrphanCheckStudent + " in its studentCollection field has a non-nullable coursegroupId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Course courseId = coursegroup.getCourseId();
            if (courseId != null) {
                courseId.getCoursegroupCollection().remove(coursegroup);
                courseId = em.merge(courseId);
            }
            Collection<Subject> subjectCollection = coursegroup.getSubjectCollection();
            for (Subject subjectCollectionSubject : subjectCollection) {
                subjectCollectionSubject.getCoursegroupCollection().remove(coursegroup);
                subjectCollectionSubject = em.merge(subjectCollectionSubject);
            }
            em.remove(coursegroup);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Coursegroup> findCoursegroupEntities() {
        return findCoursegroupEntities(true, -1, -1);
    }

    public List<Coursegroup> findCoursegroupEntities(int maxResults, int firstResult) {
        return findCoursegroupEntities(false, maxResults, firstResult);
    }

    private List<Coursegroup> findCoursegroupEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Coursegroup as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Coursegroup findCoursegroup(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Coursegroup.class, id);
        } finally {
            em.close();
        }
    }

    public int getCoursegroupCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Coursegroup as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
